﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Server.BasicsDatumServer.ProductManagement.Dto;
using SmartWMS.Server.BasicsDatumServer.SupplierManagement;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.BasicsDatumServer.ProductManagement
{
    /// <summary>
    /// 产品管理实现层
    /// </summary>
    public class ProductManagement : IProductManagement
    {
        private readonly IRepository<Product> _product;
        private readonly IRepository<Custom> _custom;
        private readonly IRepository<Location> _location;
        private readonly IRepository<Warehouse> _warehouse;
        private readonly IRepository<Unit> _unit;

        public ProductManagement(IRepository<Product> product, IRepository<Custom> custom, IRepository<Location> location, IRepository<Warehouse> warehouse, IRepository<Unit> unit)
        {
            _product = product;
            _custom = custom;
            _location = location;
            _warehouse = warehouse;
            _unit = unit;
        }

        /// <summary>
        /// 产品管理列表
        /// </summary>
        /// <param name="productManagementDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<dynamic> ProductManagementGetList(ProductSelectDto productSelectDto)
        {
            var list = _product.GetListQuery();

            if (!string.IsNullOrEmpty(productSelectDto.ProductCode))
            {
                list = list.Where(x => x.ProductCode.Contains(productSelectDto.ProductCode));
            }
            if (!string.IsNullOrEmpty(productSelectDto.ProductName))
            {
                list = list.Where(x => x.ProductName.Contains(productSelectDto.ProductName));
            }
            if (productSelectDto.ProductTypeId!=0)
            {
                list = list.Where(x => x.ProductTypeId== productSelectDto.ProductTypeId);
            }

            //分页
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / productSelectDto.pageSize);
            list = list.Skip((productSelectDto.pageIndex - 1) * productSelectDto.pageSize).Take(productSelectDto.pageSize);


            var result = list.MapToList<ProductManagementDto>();
            return Response<dynamic>.Ok(new
            {
                totalCount,
                pageCount,
                result
            });
        }

        /// <summary>
        /// 批量删除产品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> DeletesProductManagementIsDisable(ProductManagementDto dto)
        {
            var sid = dto.DelId.Split(',');
            var list = new List<Product>();
            foreach (var item in sid)
            {
                var SuppList = _product.GetListQuery().FirstOrDefault(m => m.Id == Convert.ToInt32(item));
                list.Add(SuppList);
            }
            var result = _product.DeleteAsync(list);
            return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
        }

        /// <summary>
        /// 产品管理删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteProductManagementDtoAsyne(ProductManagementDto dto)
        {
            //删除
            var info = _product.GetListQuery().FirstOrDefault(m => m.Id == dto.Id);
            var result = await _product.DeleteByIdAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }


        /// <summary>
        /// 产品管理反填
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<ProductManagementDto> GetProductManagementById(ProductManagementDto dto)
        {
            //反填
            var info = _product.GetListQuery().FirstOrDefault(m => m.Id == dto.Id);
            var result = info.MapTo<ProductManagementDto>();
            return Response<ProductManagementDto>.Ok(result);
        }





        /// <summary>
        /// 产品管理添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> ProductManagementDtoAddSave(ProductManagementDto dto)
        {
            //添加
            var info = dto.MapTo<Product>();
            var result = await _product.InsertAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }

        /// <summary>
        /// 产品管理修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> ProductManagementUpdate(ProductManagementDto dto)
        {
            //修改
            var info = dto.MapTo<Product>();
            var result = await _product.UpdateAsync(info);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }


        /// <summary>
        /// 加载客户
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Custom>>> BindCustomAsync()
        {
            var result = _custom.GetAllListAsync();
            return Response<List<Custom>>.Ok(await result);
        }

        /// <summary>
        /// 加载库位
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Location>>> BindLocationAsync()
        {
            var result = _location.GetAllListAsync();
            return Response<List<Location>>.Ok(await result);
        }

        /// <summary>
        /// 加载仓库
        /// </summary>
        /// <returns></returns>
        public async Task<Response<List<Warehouse>>> BindWarehouseAsync()
        {
            var result = _warehouse.GetAllListAsync();
            return Response<List<Warehouse>>.Ok(await result);
        }

        /// <summary>
        /// 加载单位
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<Unit>>> BindUnitAsync()
        {
            var result = _unit.GetAllListAsync();
            return Response<List<Unit>>.Ok(await result);
        }
    }
}
